﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class DataGridViewController : DataGridView
    {
        /// <summary>
        /// 调用方法实例 gridViewDetail1.SetDefaultLayout("new string[] { "FactoryName=创建时间","LastModifyTime=领料单号",}, false);
        /// </summary>
        /// <param name="dataGridView1">dataGridView</param>
        /// <param name="layoutStrings">数组</param>
        /// <param name="layReadonly">是否编辑</param>
        public static void SetDefaultLayout(DataGridView dataGridView1, string[] layoutStrings, bool layReadonly)
        {
            StringBuilder lyString = new StringBuilder();
            StringBuilder captionString = new StringBuilder();
            foreach (var a in layoutStrings)
            {
                lyString.AppendFormat(a.Substring(0, a.IndexOf("=")) + ",");
                captionString.AppendFormat(a.Substring(a.IndexOf("=") + 1, a.Length - a.IndexOf("=") - 1) + ",");
            }
            SetDefaultLayout(dataGridView1, lyString.ToString().Substring(0, lyString.ToString().LastIndexOf(",")),
                 captionString.ToString().Substring(0, captionString.ToString().LastIndexOf(",")), layReadonly);
        }
        public static void SetDefaultLayout(DataGridView dataGridView1, string layoutString, string captionString, bool layReadonly)
        {
            IList<string> visibleFields = new List<string>();
            string[] columnName = layoutString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] columnCaptions = captionString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            visibleFields = SetColumnWidth(dataGridView1, columnName, columnCaptions, layReadonly);

        }
        /// <summary>
        /// 可以编辑列
        /// </summary>
        /// <param name="dataGridView1"></param>
        /// <param name="Cellname"></param>
        public static void setDefaultCellStyle(DataGridView dataGridView1, string Cellname, bool isReadOnly)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.ClearSelection();
                if (dataGridView1.Columns[Cellname] != null)
                {
                    dataGridView1.Columns[Cellname].ReadOnly = isReadOnly;
                    dataGridView1.Columns[Cellname].DefaultCellStyle.BackColor = Color.Green;

                    dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[dataGridView1.Columns[Cellname].Index];

                    dataGridView1.BeginEdit(true);
                    dataGridView1.Rows[0].Cells[Cellname].Selected = true;
                }
            }
        }




        public static IList<string> SetColumnWidth(DataGridView dataGridView1, string[] columnName, string[] captions, bool layReadonly)
        {
            dataGridView1.Columns.Clear();
            IList<string> visibleFields = new List<string>();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.AllowUserToAddRows = false;
            for (int i = 0, max = columnName.Length; i < max; i++)
            {
                string columnLayout = columnName[i];
                string[] columnInfo = columnLayout.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                string fieldName = string.Empty;
                string columnWidth = string.Empty;
                DataGridViewTextBoxColumn gridColumn = null;
                DataGridViewCheckBoxColumn gridColumn1 = null;
                if (columnInfo.Length <= 0 || columnInfo.Length > 2)
                    continue;
                fieldName = columnInfo[0].Trim();
                if (fieldName == "select")
                {
                    gridColumn1 = new DataGridViewCheckBoxColumn();

                    if (gridColumn1 == null)
                        continue;
                    gridColumn1.DataPropertyName = fieldName;
                    gridColumn1.HeaderText = captions[i].Trim();
                    gridColumn1.Name = captions[i].Trim();
                    visibleFields.Add(fieldName.ToLower());
                    if (columnInfo.Length == 2)
                    {
                        columnWidth = columnInfo[1];
                        gridColumn1.Width = int.Parse(columnWidth);

                    }
                    gridColumn1.Width = 30;
                    gridColumn1.ReadOnly = false;
                    gridColumn1.Frozen = true;

                    dataGridView1.Columns.Add(gridColumn1);
                }
                else
                {
                    gridColumn = new DataGridViewTextBoxColumn();
                    if (gridColumn == null)
                        continue;
                    gridColumn.DataPropertyName = fieldName;
                    gridColumn.HeaderText = captions[i].Trim();
                    gridColumn.Name = captions[i].Trim();
                    visibleFields.Add(fieldName.ToLower());
                    if (columnInfo.Length == 2)
                    {

                        columnWidth = columnInfo[1];
                        if (int.Parse(columnWidth) <= 0)
                            gridColumn.Visible = false;
                        else
                            gridColumn.Width = int.Parse(columnWidth);

                    }
                    gridColumn.ReadOnly = layReadonly;
                    dataGridView1.Columns.Add(gridColumn);
                }

            }
            return visibleFields;
        }
        /// <summary>
        /// 设置dataGridView1控件是否选择中
        /// </summary>
        /// <param name="dataGridView1"></param>
        /// <param name="Column"></param>
        /// <param name="isSelect"></param>
        public static void SetDataGridViewCombox(DataGridView dataGridView1, string Column, bool isSelect)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
                row.Cells["列名"].Value = isSelect;


        }
    }
}
